﻿Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data.Common

Public Class frmWeldAR

    Sub initScreen()
        loadMaterialForCbo(cboWeldARMaterial)
        loadHeatNoForCbo(cboWeldARHeat1, cboWeldARHeat2)
        loadJointTypeForCbo(cboWeldARJointType1, cboWeldARJointType2)
        loadSizeForCbo(_PROJECT_ID, cboWeldARSize, Nothing)

        loadRejectReason(cboWeldARRejectReason, Nothing)

        clearPnlWeldAR()
        loadCouponSystemForCbo(cboWeldARSystem, _COUPON_ID)

        Me.tabWeldAR.SelectedIndex = 0
        Me.tabWeldAR.Refresh()
    End Sub

    Sub clearPnlWeldAR()
        lblARWeldLogDetailId.Text = ""

        pnlWeldReject.Visible = False
        cboWeldARRejectReason.Text = ""
        txtWeldARRejectRemark.Text = ""

        btnWeldAccept.Enabled = True
        btnWeldReject.Enabled = True

        txtRejBarcode.Text = ""
        txtWeldARBarcode.Text = ""
        txtWeldARWelder.Text = ""
        txtWeldARCoupon.Text = ""
        txtWeldARDate.Text = ""
        txtWeldARSeq.Text = ""

        cbWeldARCoax.Checked = False
        rdoWeldARPipe.Checked = True

        cboWeldARCoax.Text = ""
        cboWeldARMaterial.Text = ""
        cboWeldARSize.Text = ""
        cboWeldARThickness.Text = ""
        cboWeldARSystem.Text = ""
        cboWeldARHeat1.Text = ""
        cboWeldARHeat2.Text = ""
        cboWeldARJointType1.Text = ""
        cboWeldARJointType2.Text = ""
        txtWeldARStatus.Text = ""
    End Sub

    Private Sub tabWeldAR_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabWeldAR.SelectedIndexChanged
        If (Me.tabWeldAR.SelectedIndex = 1) Then
            refreshComboAcceptRejectWeldlogUpdateDate(cboWeldARSearchDate)
            Me.tabWeldAR.Refresh()

            lvWeldAR.Items.Clear()
        End If
    End Sub

    Private Sub txtWeldARBarcode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtWeldARBarcode.KeyPress
        If Asc(e.KeyChar) = Keys.Enter Then
            'MessageBox.Show("Enter")
            Dim barcode As String = txtWeldARBarcode.Text
            clearPnlWeldAR()
            searchWeldlogDetailForAR(barcode)
        End If
    End Sub

    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_PROJECT

        frmProject.Show()
        frmProject.initScreen()
        Me.Hide()
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        frmMenu.Show()
        frmMenu.initScreen()
        Me.Hide()
    End Sub

    Sub searchWeldlogDetailForAR(ByVal barcode As String)
        Dim lstView As New ListViewItem
        Dim dtResult As New DataTable

        dtResult = findWeldLogs(Nothing, barcode, Nothing, Nothing, Nothing, Globals.COUPON_TYPE_IN, False, Nothing, _PROJECT_ID)
        txtWeldARBarcode.Text = barcode

        If dtResult.Rows.Count > 0 Then
            Dim dataRow As DataRow = dtResult.Rows(0)

            txtWeldARWelder.Text = dataRow.Item("WELDER_NAME")
            txtWeldARCoupon.Text = dataRow.Item("COUPON_CODE")

            If (IsDBNull(dataRow.Item("REJECT_BARCODE")) = False) Then
                txtRejBarcode.Text = dataRow.Item("REJECT_BARCODE")
            Else
                txtRejBarcode.Text = ""
            End If

            txtWeldARDate.Text = dataRow.Item("WELD_DATE") & " " & dataRow.Item("WELD_TIME")

            Dim isCoas As String = ""
            Dim coax As String = ""

            txtWeldARSeq.Text = dataRow.Item("SEQ")

            Dim pipeTube As String = dataRow.Item("PIPE_TUBE")
            Dim isCoax As String = dataRow.Item("IS_COAX")

            If (isCoax = "T") Then
                cbWeldARCoax.Checked = True
            Else
                cbWeldARCoax.Checked = False
            End If

            If pipeTube = "PIPE" Then
                rdoWeldARPipe.Checked = True
            ElseIf pipeTube = "TUBE" Then
                rdoWeldARTube.Checked = True
            End If

            cboWeldARCoax.Text = dataRow.Item("COAX")
            cboWeldARMaterial.Text = dataRow.Item("MATERIAL")
            cboWeldARSize.Text = dataRow.Item("SIZE")
            cboWeldARThickness.Text = dataRow.Item("THICKNESS")
            cboWeldARSystem.Text = dataRow.Item("SYSTEM_TYPE_NAME")
            cboWeldARHeat1.Text = dataRow.Item("HEAT_NUMBER_NAME_1")
            cboWeldARHeat2.Text = dataRow.Item("HEAT_NUMBER_NAME_2")
            cboWeldARJointType1.Text = dataRow.Item("JOINT_TYPE_NAME_1")
            cboWeldARJointType2.Text = dataRow.Item("JOINT_TYPE_NAME_2")
            txtWeldARStatus.Text = dataRow.Item("STATUS_CODE")

            btnWeldAccept.Enabled = True
            btnWeldReject.Enabled = True

            lblARWeldLogDetailId.Text = dataRow.Item("WELDLOG_DETAIL_ID")
        Else
            MsgBox("Barcode not found.", MsgBoxStyle.Information, "EQAM")
            clearPnlWeldAR()

            btnWeldAccept.Enabled = False
            btnWeldReject.Enabled = False
        End If
    End Sub

    Private Sub btnWeldAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldAccept.Click
        Dim returnExecute As Boolean = False
        Dim rejectReason As String = ""
        Dim statusCode As String = ""

        If (lblARWeldLogDetailId.Text = "") Then
            MsgBox("Please rescan. Barcode is not exist.", MsgBoxStyle.Information, "EQAM")
            Return
        End If


        statusCode = Globals.WELDLOG_STATUS_ACCEPT
        rejectReason = ""

        Dim heatId1 As String = ""
        Dim heatName1 As String = cboWeldARHeat1.Text
        Dim heatId2 As String = ""
        Dim heatName2 As String = cboWeldARHeat2.Text

        Dim dtResult As New DataTable
        dtResult = findHeatNos(Nothing, cboWeldARHeat1.Text)
        If dtResult.Rows.Count > 0 Then
            heatId1 = dtResult.Rows(0).Item("HEAT_NUMBER_ID")
        End If

        dtResult = findHeatNos(Nothing, cboWeldARHeat2.Text)
        If dtResult.Rows.Count > 0 Then
            heatId2 = dtResult.Rows(0).Item("HEAT_NUMBER_ID")
        End If

        Dim jointTypeCode1 As String = ""
        Dim jointTypeName1 As String = cboWeldARJointType1.Text
        Dim jointTypeCode2 As String = ""
        Dim jointTypeName2 As String = cboWeldARJointType2.Text
        dtResult = findJointTypes(cboWeldARJointType1.Text)
        If dtResult.Rows.Count > 0 Then
            jointTypeCode1 = dtResult.Rows(0).Item("JOINT_TYPE_CODE")
        End If

        dtResult = findJointTypes(cboWeldARJointType2.Text)
        If dtResult.Rows.Count > 0 Then
            jointTypeCode2 = dtResult.Rows(0).Item("JOINT_TYPE_CODE")
        End If

        Dim systemId As String = ""
        Dim systemName As String = cboWeldARSystem.Text
        dtResult = findSystemTypes(Nothing, cboWeldARSystem.Text)
        If dtResult.Rows.Count > 0 Then
            systemId = dtResult.Rows(0).Item("SYSTEM_TYPE_ID")
        End If

        Dim seq As Integer = Nothing
        If (txtWeldARSeq.Text <> "") Then
            Try
                seq = Convert.ToDecimal(txtWeldARSeq.Text)
            Catch ex As Exception
                'seq
            End Try
        End If

        returnExecute = acceptWeldLog(systemId, systemName, heatId1, heatName1, heatId2, heatName2, jointTypeCode1, jointTypeName1, jointTypeCode2, jointTypeName2, seq, lblARWeldLogDetailId.Text)

        If returnExecute = True Then
            MsgBox(_SAVE_SUCCESSFUL_MESSAGE, MsgBoxStyle.Information, "EQAM")
            clearPnlWeldAR()
        Else
            'MessageBox.Show("Error!")
            Return
        End If
    End Sub

    Private Sub btnWeldReject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldReject.Click
        If (lblARWeldLogDetailId.Text = "") Then
            MsgBox("Please rescan. Barcode is not exist.", MsgBoxStyle.Information, "EQAM")
            Return
        End If

        pnlWeldReject.Visible = True
        btnWeldAccept.Enabled = False
        btnWeldReject.Enabled = False

        pnlWeldReject.Location = New System.Drawing.Point(5, 69)
        pnlWeldReject.Size = New System.Drawing.Size(209, 215)
    End Sub

    Private Sub cboWeldARRejectClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWeldARRejectClose.Click
        pnlWeldReject.Visible = False
        btnWeldAccept.Enabled = True
        btnWeldReject.Enabled = True
    End Sub

    Private Sub btnWeldARRejectSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldARRejectSave.Click
        Dim returnExecute As Boolean = False
        Dim rejectReason As String = ""
        Dim statusCode As String = ""

        statusCode = Globals.WELDLOG_STATUS_REJECTED
        rejectReason = cboWeldARRejectReason.Text

        If (rejectReason = "") Then
            MsgBox("Reject Reason is empty.", MsgBoxStyle.Information, "EQAM")
            Return
        End If

        returnExecute = updateWeldStatus(txtWeldARRejectRemark.Text, rejectReason, statusCode, lblARWeldLogDetailId.Text)

        If returnExecute = True Then
            MsgBox(_SAVE_SUCCESSFUL_MESSAGE, MsgBoxStyle.Information, "EQAM")
            clearPnlWeldAR()
        Else
            'MessageBox.Show("Error!")
            Return
        End If
    End Sub

    Private Sub cboWeldARSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWeldARSize.SelectedIndexChanged
        If (cboWeldARSize.Text <> "" And cboWeldARJointType1.Text() <> "") Then
            Dim heatNumber As String = fetchHeatNumberByWeldSizeJointType(_PROJECT_ID, cboWeldARSize.Text, cboWeldARJointType1.Text)
            cboWeldARHeat1.Text = heatNumber
        End If
        If (cboWeldARSize.Text <> "" And cboWeldARJointType2.Text() <> "") Then
            Dim heatNumber As String = fetchHeatNumberByWeldSizeJointType(_PROJECT_ID, cboWeldARSize.Text, cboWeldARJointType2.Text)
            cboWeldARHeat2.Text = heatNumber
        End If
    End Sub

    Private Sub cboWeldARJointType1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWeldARJointType1.SelectedIndexChanged
        If (cboWeldARSize.Text <> "" And cboWeldARJointType1.Text() <> "") Then
            Dim heatNumber As String = fetchHeatNumberByWeldSizeJointType(_PROJECT_ID, cboWeldARSize.Text, cboWeldARJointType1.Text)
            cboWeldARHeat1.Text = heatNumber
        End If
    End Sub

    Private Sub cboWeldARJointType2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWeldARJointType2.SelectedIndexChanged
        If (cboWeldARSize.Text <> "" And cboWeldARJointType2.Text() <> "") Then
            Dim heatNumber As String = fetchHeatNumberByWeldSizeJointType(_PROJECT_ID, cboWeldARSize.Text, cboWeldARJointType2.Text)
            cboWeldARHeat2.Text = heatNumber
        End If
    End Sub

    Private Sub btnWeldARSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldARSearch.Click
        Dim status As String = ""
        If (cboWeldARSearchStatus.Text = "Accept") Then
            status = Globals.WELDLOG_STATUS_ACCEPT
        ElseIf (cboWeldARSearchStatus.Text = "Reject") Then
            status = Globals.WELDLOG_STATUS_REJECTED
        End If
        findAcceptRejectWeldLogForListing(txtWeldARSearchBarcode.Text, cboWeldARSearchDate.Text, status, lvWeldAR)
    End Sub

    Private Sub btnWeldAREdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldAREdit.Click
        Dim sic As ListView.SelectedIndexCollection
        sic = lvWeldAR.SelectedIndices()
        Dim selectedBarcode As String = ""

        If sic.Count <> 0 Then
            selectedBarcode = lvWeldAR.Items(sic.Item(0)).Tag

            clearPnlWeldAR()
            searchWeldlogDetailForAR(selectedBarcode)

            Me.tabWeldAR.SelectedIndex = 0
            Me.tabWeldAR.Refresh()
        End If
    End Sub
End Class